<?php

namespace App\Admin\Controllers;

use App\Models\Ass;
use App\Models\Obj;
use App\Models\Org;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Grid\Displayers\Actions;

class AssController extends AdminController
{
    protected $title = '工作记录管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Ass());

        $orgIds = Org::where('parent_id', '=', Admin::user()->org_id)->pluck('id')->toArray();
        $orgIds[] = Admin::user()->org_id;
        $grid->model()->whereIn('org_id', $orgIds)->orderBy('id', 'desc');

        $grid->no('工作编号')->style('min-width:100px;');
        $grid->member_names('执行人')->style('min-width:150px;');
        $grid->column('org.title', '所属单位')->style('min-width:100px;');
        $grid->column('ooo', '执行对象')->display(function ($column) {

            // 如果这一列的status字段的值等于1，直接显示title字段
            if ($this->type == 1) {
                $obj = Obj::find($this->obj_id);
                return empty($obj) ? '无' : $obj->title;
            }else{
                $obj = Org::find($this->work_org_id);
                return empty($obj) ? '无' : $obj->title;
            }

        })->style('min-width:100px;');
        $grid->column('type', '工作类型')->using([
            0 => '全部工作',
            1 => '结对帮扶',
            2 => '执法检查',
            3 => '监督检查',
        ])->sortable()->style('min-width:100px;');

//        $grid->content('文字内容')->style('min-width:200px;');

        // 显示多图
//        $grid->column('imgs', '图片内容')->carousel(150, 150, $_SERVER['HTTP_HOST'])->style('min-width:200px;');

        $grid->column('member.name', '上报人')->style('min-width:100px;');


        $grid->column('created_at', '上报时间')->sortable()->style('min-width:100px;');

        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableCreateButton();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->expand();
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
                $filter->like('member_names', '执行人姓名');
                $filter->equal('org_id', '所属单位')->select(Org::where('type', 0)->pluck('title', 'id'));
            });

            $filter->column(1/3, function ($filter) {
                $filter->between('created_at', '上报日期')->date();

            });

            $filter->column(1/3, function ($filter) {
                $filter->equal('type', '工作类型')->select([
                    '' => '全部工作',
                    1 => '结对帮扶',
                    2 => '执法检查',
                    3 => '监督检查',
                ]);
            });
        });

        $grid->actions(function ($actions) {
            // 去掉编辑
            $actions->disableEdit();
            // 去掉查看
//            $actions->disableView();
        });

        $grid->setActionClass(Actions::class);

        return $grid;
    }

    protected function detail($id)
    {
        $row = Ass::findOrFail($id);
        $show = new Show($row);

        $show->field('no', '工作编号');
        $show->field('member_names', '执行人');
        $show->field('org.title', '所属单位');

//        $show->field('ooo', '执行对象')->display(function ($column) {
//
//            // 如果这一列的status字段的值等于1，直接显示title字段
//            if ($this->type == 1) {
//                $obj = Obj::find($this->obj_id);
//                return empty($obj) ? '无' : $obj->title;
//            }else{
//                $obj = Org::find($this->work_org_id);
//                return empty($obj) ? '无' : $obj->title;
//            }
//
//        });

        $show->field('ooo', '执行对象')->as(function ()use($row) {
            if ($row->type == 1) {
                $obj = Obj::find($row->obj_id);
                return empty($obj) ? '无' : $obj->title;
            }else{
                $obj = Org::find($row->work_org_id);
                return empty($obj) ? '无' : $obj->title;
            }
        });

        $show ->field('type', '工作类型')->using([
        1 => '结对帮扶',
        2 => '执法检查',
        3 => '监督检查',
    ]);
        $show->field('member.name', '上报人');
        $show->field('created_at', '上报时间');
        $show->field('content', '文字内容');
        $show->field('imgs', '图片内容')->carousel(500, 500, $_SERVER['HTTP_HOST'])->style('min-width:500px;min-height:500px;');

//        $show->field('images')->carousel($width = 300, int $height = 200, $server);


        $show->field('坐标')->latlong('lat', 'lng', $height = 500, $zoom = 16);


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Ass());

        $orgIds = Org::where('parent_id', '=', Admin::user()->org_id)->pluck('id')->toArray();
        $orgIds[] = Admin::user()->org_id;

        $form->display('id', 'ID');

        $form->text('no', '工作编号')->rules('required');
        $form->text('member_names', '执行人')->rules('required');
        $form->select('obj_id', '执行对象')->options(Obj::whereIn('org_id', $orgIds)->pluck('title', 'id'));
        $form->textarea('content', '文字内容')->rules('required');


        $form->latlong('lat', 'lng', '坐标')->width(800)->height(500)->zoom(16)->default(['lat' => 30.642567874701466, 'lng' => 104.04522657394409]);


        $form->saved(function (Form $form) {
            Ass::where('id', $form->model()->id)->update([
                'member_id' => Admin::user()->id,
                'org_id' => Admin::user()->org_id,
            ]);
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });

        return $form;
    }
}
